package cn.kgc.crowdfundingadminspringboot2.service;


import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadminspringboot2.mapper.T_adminMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.T_admin;
import cn.kgc.crowdfundingadminspringboot2.pojo.T_adminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService{

    @Resource
    T_adminMapper t_adminMapper;

    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public T_admin selectById(Integer id) {
        return t_adminMapper.selectByPrimaryKey(id);
    }

    /**
     * 登录
     * @param loginAcct
     * @param userPswd
     * @return   用户名密码正确返回Admin对象   否则返回null
     */
    @Override
    public T_admin login(String loginAcct, String userPswd) {
        T_adminExample adminExample=new T_adminExample();
        adminExample.createCriteria().andLoginacctEqualTo(loginAcct);
        List<T_admin> list = t_adminMapper.selectByExample(adminExample);
        if (CrowdFundingUtils.collectionEffective(list)==false){
            return null;
        }
        T_admin admin = list.get(0);
        if (admin==null){
            return null;
        }

        String md5 = CrowdFundingUtils.md5(userPswd);
        if (Objects.equals(md5,admin.getUserpswd())){
            return admin;
        }
        return null;
    }

    @Override
    public PageInfo<T_admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<T_admin> pageInfo=null;
        if (keyword!=null&&keyword.length()>0){
            T_adminExample example=new T_adminExample();
            T_adminExample.Criteria criteria = example.createCriteria();
            criteria.andLoginacctLike("%"+keyword+"%");

            T_adminExample.Criteria criteria2=example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            example.or(criteria2);

            T_adminExample.Criteria criteria3=example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");
            example.or(criteria3);
            List<T_admin> list=t_adminMapper.selectByExample(example);
            pageInfo=new PageInfo<>(list);
        }else{
            List<T_admin> list=t_adminMapper.selectByExample(null);
            pageInfo=new PageInfo<>(list);
        }
        return pageInfo;
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        T_adminExample example=new T_adminExample();
        T_adminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);

        t_adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(T_admin admin) {
        String encode = bCryptPasswordEncoder.encode(admin.getUserpswd());
//        String userPswd = admin.getUserpswd();
//        userPswd = CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(encode);

        // 执行保存
        t_adminMapper.insert(admin);
    }

    @Override
    public T_admin getAdminById(Integer adminId) {
        return t_adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(T_admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String encode = bCryptPasswordEncoder.encode(admin.getUserpswd());
            admin.setUserpswd(encode);
        }
        t_adminMapper.updateByPrimaryKeySelective(admin);
    }
}
